<template>
	<view v-if="goods_info.goods_name">
		<view class="goods-detail-container">
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
				<swiper-item v-for="(item ,i) in goods_info.pics" :key="i">
					<image :src="item.pics_big_url" @click="preview(i)"></image>
				</swiper-item>
				
			</swiper>
		</view>
		<!--商品信息-->
		<view class="goods-info-box">
			<!--商品价格-->
			<view class="price">￥{{goods_info.goods_price}}</view>
			<!--信息主体区域-->
			<view class="goods-info-body">
				<!--商品名称-->
				<view class="goods-name">{{goods_info.goods_name}}</view>
				<!--收藏-->
				<view class="fanvi">
					<uni-icons type="star" size="16" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!--运费-->
			<view class="yf">快递，免运费</view>
		</view>
		<rich-text :nodes="goods_info.goods_introduce"></rich-text>
		<!--商品导航组件区域-->
		<view class="goods-nav">
			<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	//从vuex中按需到处mapState辅助方法
	import {mapState,mapMutations,mapGetters} from 'vuex'
	
	export default {
		computed:
		{
			// // 调用 mapState 方法，把 m_cart 模块中的 cart 数组映射到当前页面中，作为计算属性来使用
			//  // ...mapState('模块的名称', ['要映射的数据名称1', '要映射的数据名称2'])
			// ...mapState('cart',[cart]),
			 ...mapState('cart',['cart']),
			// 把 m_cart 模块中名称为 total 的 getter 映射到当前页面中使用
			...mapGetters('cart',['total']),
		},
		data() {
			return {
				goods_info:{},
				options: [{
							icon: 'headphones',
							text: '客服'
						}, {
							icon: 'shop',
							text: '店铺',
							info: 0,
							infoBackgroundColor:'#007aff',
							infoColor:"red"
						}, {
							icon: 'cart',
							text: '购物车',
							info: 0
						}],
					    buttonGroup: [{
					      text: '加入购物车',
					      backgroundColor: '#ff0000',
					      color: '#fff'
					    },
					    {
					      text: '立即购买',
					      backgroundColor: '#ffa200',
					      color: '#fff'
					    }
					    ]
			}
		},
		onLoad(option) {
			
			const goods_id=option.goods_id;
			this.getGoodList(goods_id);
			
		},
		methods: {
			///把mcart模块中的addCart 方法映射到当前的页面
			...mapMutations('cart',['addCart']),
			async  getGoodList(goods_id)
			{
				const res=await  uni.$http.get("/api/public/v1/goods/detail",{goods_id:goods_id})
				if(res.statusCode!==200)
				{
					return uni.showMsg();
				}
				this.goods_info=res.data.message;
				this.goods_info.goods_introduce.replace(/<img /g,'<img style="display: block;" ').replace(/webp/g,'jpg');
			},
			preview(i)
			{
				//图片点击放大
				uni.previewImage({
					current:i,
					urls:this.goods_info.pics.map(x=>x.pics_big)
				})
			},onClick (e) {
				if(e.content.text==='购物车'){
					uni.switchTab({
						url:'/pages/cart/cart'
					})
				}
			  },
			  buttonClick (e) {
				if(e.content.text==='加入购物车')
				{
					//组织一个商品的信息列表
					const goods={
						goods_id:this.goods_info.goods_id,//商品id
						goods_name:this.goods_info.goods_name,///商品名称
						goods_price:this.goods_info.goods_price,//商品价格
						goods_count:1,//商品数量
						goods_small_logo:this.goods_info.goods_small_logo,//、商品图片
						goods_state:true//商品勾选状态
					}
					///通过this映射过来的addCart方法，把商品信息对象存储到购物车中
					this.addCart(goods)
				}
				
			  }
		},
		watch:{
			total:
			{
				// handler 属性用来定义侦听器的 function 处理函数
				handler(newVal)
				{
					const findResult=this.options.find((x)=>x.text==='购物车')
					if(findResult)
					{
						findResult.info=newVal;
					}
				},
				 // immediate 属性用来声明此侦听器，是否在页面初次加载完毕后立即调用
				immediate:true
			}
		}
	

	}
</script>

<style lang="scss">
.goods-detail-container
{
	padding-bottom: 50px;
}
 swiper
 {
	 height: 650rpx;
	 image
	 {
		 width: 100%;
		 height: 100%;
	 }
 }
 .goods-info-box
 {
	 padding: 10px;
	 padding-right: 0px;
	 .price
	 {
		 font-size: 16px;
		 color:#C00000;
	 }
	 .goods-info-body{
		 display: flex;
		 margin-right: 10px;
		 justify-content: space-between;
		 align-items: center;
	 }
	 .goods-name
	 {
		 font-size: 13px;
		 margin-right: 10px;
	 }
	 .fanvi
	 {
		 width: 120px;
		 font-size: 12px;
		 display: flex;
		 flex-direction: column;
		 justify-content: center;
		 align-items: center;
		 border-left: 1px solid #efefef;
		 color:gray;
		 
	 }
	 .yf
	 {
		 font-size: 12px;
		 color: gray;
		 margin: 10px 0;
	 }
	 .goods-nav
	 {
		 position: fixed;
		 bottom: 0;
		 left: 0;
		 width: 100%;
	 }
 }
</style>
